from pydantic import Extra

from apitest.engine.client.http import HttpClient
from apitest.engine.const.enums import RType
from apitest.engine.const.model import Auth, EnvModel
from apitest.engine.parse import parse_request
from apitest.lib.passport_lib import model
from apitest.lib.passport_lib.route import PaasPassportAuthRoute

PSM = "paas.passport.auth"


class ByteCloudJWTAPIService(HttpClient):

    def __init__(self, app: str, auth: Auth = None, env: EnvModel = None, rtype: RType = RType.obj, **kwargs):
        super(ByteCloudJWTAPIService, self).__init__(app=app, psm=PSM, auth=auth, env=env, **kwargs)
        self.rtype = rtype

    @parse_request
    def GetAuthApiV1Jwt(self, rtype: RType = RType.obj, extra: Extra = Extra.ignore, **kwargs):
        resp = self.client.get(PaasPassportAuthRoute.AuthApiV1Jwt, **kwargs)
        if rtype == RType.obj:
            model.GetAuthApiV1JwtResponse.Config.extra = extra
            return model.GetAuthApiV1JwtResponse(**resp.json())
        elif rtype == RType.json:
            return resp.json()
        return resp

    @parse_request
    def GetAuthApiV1JwtOnesite(self, rtype: RType = RType.obj, extra: Extra = Extra.ignore, **kwargs):
        resp = self.client.get(PaasPassportAuthRoute.ApiV1IdentityJwt, **kwargs)
        if rtype == RType.obj:
            model.GetAuthApiV1JwtResponse.Config.extra = extra
            return model.GetAuthApiV1JwtResponse(**resp.json())
        elif rtype == RType.json:
            return resp.json()
        return resp

    @parse_request
    def AuthorizeHandlerLark(self, rtype: RType = RType.obj, extra: Extra = Extra.ignore, **kwargs):
        resp = self.client.post(PaasPassportAuthRoute.AuthApiV1AuthorizeLark, **kwargs)
        if rtype == RType.obj:
            model.AuthorizeHandlerLarkResp.Config.extra = extra
            return model.AuthorizeHandlerLarkResp(**resp.json())
        elif rtype == RType.json:
            return resp.json()
        return resp

    @parse_request
    def GetJWKS(self, rtype: RType = RType.obj, extra: Extra = Extra.ignore, **kwargs):
        resp = self.client.get(PaasPassportAuthRoute.ApiV1IdentityJwks, **kwargs)
        if rtype == RType.obj:
            model.GetJWKSResp.Config.extra = extra
            return model.GetJWKSResp(**resp.json())
        elif rtype == RType.json:
            return resp.json()
        return resp

    @parse_request
    def GetByteCloudJWKS(self, rtype: RType = RType.obj, extra: Extra = Extra.ignore, **kwargs):
        resp = self.client.get(PaasPassportAuthRoute.AuthApiV1Jwks, **kwargs)
        if rtype == RType.obj:
            model.GetJWKSResp.Config.extra = extra
            return model.GetJWKSResp(**resp.json())
        elif rtype == RType.json:
            return resp.json()
        return resp

    @parse_request
    def OAuthToken(self, rtype: RType = RType.obj, extra: Extra = Extra.ignore, **kwargs):
        resp = self.client.post(PaasPassportAuthRoute.AuthApiV1Token, **kwargs)
        if rtype == RType.obj:
            model.OAuthTokenResp.Config.extra = extra
            return model.OAuthTokenResp(**resp.json())
        elif rtype == RType.json:
            return resp.json()
        return resp

    @parse_request
    def OAuthTokenProxy(self, rtype: RType = RType.obj, extra: Extra = Extra.ignore, **kwargs):
        resp = self.client.post(PaasPassportAuthRoute.AuthApiV1TokenProxy, **kwargs)
        if rtype == RType.obj:
            model.OAuthTokenResp.Config.extra = extra
            return model.OAuthTokenResp(**resp.json())
        elif rtype == RType.json:
            return resp.json()
        return resp

    @parse_request
    def ShiftOneSiteJwt(self, rtype: RType = RType.obj, extra: Extra = Extra.ignore, **kwargs):
        resp = self.client.post(PaasPassportAuthRoute.ApiV1IdenityShift, **kwargs)
        if rtype == RType.obj:
            model.ShiftOneSiteJwtResp.Config.extra = extra
            return model.ShiftOneSiteJwtResp(**resp.json())
        elif rtype == RType.json:
            return resp.json()
        return resp

    # @parse_request
    # def PublicKey(self, rtype: RType = RType.obj, extra: Extra = Extra.ignore, **kwargs):
    #     resp = self.client.get(PaasPassportAuthRoute.AuthApiV1PublicKey, **kwargs)
    #     if rtype == RType.obj:
    #         model.PublicKeyReq.Config.extra = extra
    #         return model.PublicKeyReq(**resp.json())
    #     elif rtype == RType.json:
    #         return resp.json()
    #     return resp
